/**
 * Spell check
 */
const { Menu, MenuItem } = require('electron')

module.exports = (win) => {
  win.webContents.on('context-menu', (event, params) => {
    const menu = new Menu()
    console.info('received context menu event: ', params)
    for (const suggestion of params.dictionarySuggestions) {
      menu.append((new MenuItem({
        label: suggestion,
        click: () => win.webContents.replaceMisspelling(suggestion)
      })))
    }

    if (params.misspelledWord) {
      menu.append(new MenuItem({
        label: 'Add to dictionary',
        click: () => win.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
      }))
    }

    menu.popup()
  })
  win.webContents.session.listWordsInSpellCheckerDictionary().then((value) => {
    console.info('spell checker dictionary words: ', value)
  })
}